package com.qg.cyapp.component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.ScrollView;

import com.qg.cyapp.R;

public abstract class WaterFall extends ScrollView {
	private String TAG = "WaterFall";

	private LinearLayout llContent, llRoot, svChild;
	private View headView, footView;

	private Context context;
	private LayoutInflater inflater;

	int phoneWidth;
	private int columnCount = 0;
	private int oldChildCount = 0;
	// 一次加载的个数
	private int oneShow;
	private LinearLayout[] columnArr;
	// 记录没一列的总高度
	private long[] heightArr;
	// 记录每一列的View的高度
	private Map<Integer, Integer>[] maHeight;
	// 记录每一列View的顶点Top
	private Map<Integer, Integer>[] maTop;
	// 记录每一列的编号和View
	private Map<Integer, View>[] maView;
	// 未显示的View
	private List<View> scrapViews = new ArrayList<View>();

	public WaterFall(Context context) {
		// TODO Auto-generated constructor stub
		super(context);
		setup(context);
		WindowManager wm = ((Activity) context).getWindowManager();
		phoneWidth = wm.getDefaultDisplay().getWidth();
	}

	public WaterFall(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		setup(context);
	}

	private void setup(Context context) {
		this.context = context;
		this.oneShow = getShowNum();
		inflater = LayoutInflater.from(context);
		svChild = (LinearLayout) inflater.inflate(
				R.layout.item_water_fall_content, null);
		this.llContent = (LinearLayout) svChild.findViewById(R.id.ll_content);
		this.addView(svChild);

	}

	public void notifyDataSetChanged() {
		int newChildCount = getContentChildCount();
		View child;
		// 从最旧的下一个position开始,添加,这样就要保证旧的是没有改变的
		// 每次加载oneShow = getShowNum()个View
		for (int i = oldChildCount, num = oldChildCount + oneShow; i < newChildCount
				&& i < num; i++) {
			child = getView(i);
			addColumnView(child, getHeight(i));
		}
//		measureViews();
	}

	public void removeAllViews() {
		// llContent.removeAllViews();
		for (int i = 0; i < columnCount; i++) {
			columnArr[i].removeAllViews();
			heightArr[i] = 0;
			maHeight[i].clear();
			maView[i].clear();
			maTop[i].clear();
		}
		oldChildCount = 0;
	}

	public final void deleteChildAt(int position) {

		for (int i = 0; i < columnCount; i++) {
			if (maView[i].containsKey(position)) {
				columnArr[i].removeView(maView[i].get(position));
				heightArr[i] -= maHeight[i].get(position);
				deleteViewMa(i, position);
				break;
			}
		}
		oldChildCount -= 1;

	}

	public int getHeight(int position) {
		return 1;
	}

	private void addColumnView(View view, int height) {
		int minHeight = 0;
		for (int i = 1; i < columnCount; i++) {
			if (heightArr[i] < heightArr[minHeight]) {
				minHeight = i;
			}
		}
		columnArr[minHeight].addView(view);
		maView[minHeight].put(oldChildCount, view);
		oldChildCount++;
		heightArr[minHeight] += height;
	}

	private void addViewMa(int column, int position, int height, View child) {
		maHeight[column].put(position, height);
		maView[column].put(position, child);
	}

	private void deleteViewMa(int column, int position) {
		maHeight[column].remove(position);
		maView[column].remove(position);
	}

	public final void init(int columnCount) {
		if (columnCount > 0) {
			columnArr = new LinearLayout[columnCount];
			heightArr = new long[columnCount];
			maHeight = new HashMap[columnCount];
			maView = new HashMap[columnCount];
			maTop = new HashMap[columnCount];
			this.columnCount = columnCount;
			for (int i = 0; i < columnCount; i++) {
				LinearLayout llColumn = new LinearLayout(context);
				int width = phoneWidth / columnCount;
				LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
						width, LinearLayout.LayoutParams.WRAP_CONTENT);
				llColumn.setOrientation(LinearLayout.VERTICAL);
				llContent.addView(llColumn, params);
				columnArr[i] = llColumn;
				heightArr[i] = 0;
				maHeight[i] = new HashMap<Integer, Integer>();
				maView[i] = new HashMap<Integer, View>();
				maTop[i] = new HashMap<Integer, Integer>();
			}
		}
	}

	// 返回列数
	public int getColumnCount() {
		return columnCount;
	}

	// 在顶部添加一个View
	public final void addHeadView(View view) {
		svChild.addView(view, 0);
	}

	// 在底部添加一个View
	public final void addFootView(View view) {
		svChild.addView(view);
	}

	// public final void addViewInColumn(int column, View view, int height) {
	// if (column < columnCount) {
	// columnArr[column].addView(view);
	// maHeight[column].put(key, value)
	// heightArr[column] += height;
	// }
	// }

	@Override
	protected void onScrollChanged(int l, int t, int oldl, int oldt) {
		// TODO Auto-generated method stub
		// 滑到底部时,调用scrollToBottom()
		if (svChild.getMeasuredHeight() <= getScrollY() + getHeight()) {
			scrollToBottom();
		}
		super.onScrollChanged(l, t, oldl, oldt);
	}

	public void scrollToBottom() {
		notifyDataSetChanged();
	}

	// 返回显示的View的起始行
	public void measureViews() {
		View view;
		int position;
		int row = 0;
		while (row < columnCount) {
			Iterator<Entry<Integer, View>> iterator = maView[row].entrySet()
					.iterator();
			Log.v(TAG, "第 " + row + "列");
			while (iterator.hasNext()) {
				Entry<Integer, View> entry = iterator.next();
				view = entry.getValue();
				position = entry.getKey();
				maTop[row].put(position, view.getTop());
				Log.v(TAG, "position  " + position + " Top : " + view.getTop());
			}
			row++;
		}

	}

	// notifyDataSetChange时调用来获取子View
	// 该方法实现,用于返回子View
	public abstract View getView(int position);

	// 每个View的唯一标识,一般为每个View的数据源
	public abstract Object getObject(int position);

	// 获得WaterFall中子View的数量
	public abstract int getContentChildCount();

	// 一屏显示的的数量
	public abstract int getShowNum();

}
